package com.wulian.controller;

import com.google.rpc.Code;
import com.wulian.entities.Record;
import com.wulian.result.CodeStatus;
import com.wulian.result.ResponseResult;
import com.wulian.service.FabricService;
import com.wulian.service.RecordService;
import com.wulian.utils.IDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hyperledger.fabric.gateway.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * @author xf
 * @date 2021/3/21 下午 3:04
 * @Description
 */
@Api(tags = "病历操作接口")
@RequestMapping("/record")
@RestController
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private FabricService fabricService;

    @ApiOperation("查询所有病历")
    @GetMapping("/")
    public ResponseResult<List<Record>> getAll(){
        try {
            List<Record> records = recordService.getAll();
            if (records!=null){
                return new ResponseResult<>(CodeStatus.OK,"查询成功",records);
            }else {
                return new ResponseResult<>(CodeStatus.NOT_FOUND,"未查询到相关信息");
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e.getMessage());
        }
    }

    @ApiOperation("分页查询病历")
    @GetMapping("/{pageNum}/{size}")
    public ResponseResult<List<Record>> getAllByPage(@PathVariable Integer pageNum,@PathVariable Integer size){
        if(pageNum<=0&&size<=0){
            return new ResponseResult<>(CodeStatus.ILLEGAL_REQUEST,"参数错误");
        }
        try {
            List<Record> records = recordService.getAllByPage(pageNum, size);
            if (records!=null){
                return new ResponseResult<>(CodeStatus.OK,"查询成功",records);
            }else {
                return new ResponseResult<>(CodeStatus.NOT_FOUND,"未查询到相关信息");
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }

    @ApiOperation("根据编号查询病历")
    @GetMapping("/{id}")
    public ResponseResult<Record> getRecordById(@PathVariable Long id){
        try {
            Record record = recordService.getRecordById(id);
            if (record!=null){
                return new ResponseResult<>(CodeStatus.OK,"查询成功",record);
            }else {
                return new ResponseResult<>(CodeStatus.NOT_FOUND,"未查询到相关信息");
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }

    @ApiOperation("上传病历")
    @PostMapping("/")
    public ResponseResult<Object> addRecord(@RequestBody Record record){
        try {
            Long recordId = IDUtil.makeRecordId();
            record.setRecord_id(Long.valueOf(recordId));
            recordService.addRecord(record);
            fabricService.addRecordToFabric(record);
            return new ResponseResult<>(CodeStatus.OK,"添加成功");
        } catch (IOException e) {
            return new ResponseResult<>(CodeStatus.FAIL,e.getMessage());
        } catch (CertificateException e) {
            return new ResponseResult<>(CodeStatus.FAIL,e.getMessage());
        } catch (TimeoutException e) {
            return new ResponseResult<>(CodeStatus.FAIL,e.getMessage());
        } catch (InterruptedException e) {
            return new ResponseResult<>(CodeStatus.FAIL,e.getMessage());
        } catch (InvalidKeyException e) {
            return new ResponseResult<>(CodeStatus.FAIL,e.getMessage());
        } catch (ContractException e) {
            return  new ResponseResult<>(CodeStatus.OK,e.getMessage());
        }

    }

    @ApiOperation("修改病历")
    @PutMapping("/")
    public ResponseResult<Object> updateRecord(@RequestBody Record record){
        try {
            System.out.println(record);
            recordService.updateRecord(record);
            return new ResponseResult<>(CodeStatus.OK,"修改成功");
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }

    @ApiOperation("删除病历")
    @DeleteMapping("/{id}")
    public ResponseResult<Object> deleteRecord(@PathVariable Long id){
        try {
            recordService.deleteRecord(id);
            return new ResponseResult<>(CodeStatus.OK,"删除成功");
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);

        }
    }

}
